package org.terracotta.toolkit.samples;

import org.terracotta.api.ClusteringToolkit;
import org.terracotta.api.TerracottaClient;
import org.terracotta.coordination.Barrier;

/**
 *
 */
public class ClusteredBarrierSample {

    static final String BARRIER_NAME = "RUN_BARRIER";

    Barrier barrier;

    // Handle to the toolkit
    ClusteringToolkit clusterToolkit = null;

    public void initializeToolKit(String serverAdd, int barrierParties) {
        clusterToolkit = new TerracottaClient(serverAdd).getToolkit();
        barrier = clusterToolkit.getBarrier(BARRIER_NAME, barrierParties);
    }

    /**
     * waits till all the parties have joined and once all drivers are ready
     * fires the test
     */
    public void fireTest() {
        try {
            barrier.await();
            // fire my network drivers
            System.out.println("firing the driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ClusteredBarrierSample barrierSample = new ClusteredBarrierSample();
        barrierSample.initializeToolKit(args[0], Integer.parseInt(args[1]));
        barrierSample.fireTest();
    }
}
